<?php

include_once('global.php');
include_once('session.php');
include_once('dom.php');
include_once('login.php');


//--------------------------------------------------------------------------------
// When an user as no cover, get one by default
//--------------------------------------------------------------------------------
function getFirstCover($pseudo) {
        $query2 = "SELECT name FROM images WHERE user='$pseudo' ORDER BY cat,ordinal,name";
        debugSQL($query2);
        $rs2 = mysql_query( $query2 );
        if( $rs2 > 0 ) 
          list($cover) = mysql_fetch_row ($rs2);
	return $cover;
}


//--------------------------------------------------------------------------------
// Show the page for choosing the album cover
//--------------------------------------------------------------------------------
function chooseCover() {
  global $sessionid;
  global $pseudo;
  global $dom;
  debug("Showing Choose Cover page");

  $xroot = $dom->documentElement;
  $xchoosecover  = $dom->createElement("choosecover");
  
  $ximages = $dom->createElement("images");
  $query = "SELECT name, cat, ordinal, candidate FROM images ";
  $query .= " WHERE user='$pseudo' ORDER BY cat, ordinal, name";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) {
    while( list($name, $cat, $ordinal, $candidate) = mysql_fetch_row ($rs) ) {
      $cat = (int)$cat;
      $candidate = (int)$candidate;
      $ximage = $dom->createElement("image");
      $ximage->setAttribute("name", $name);
      $ximage->setAttribute("cat", $cat);
      $ximage->setAttribute("candidate", $candidate);
      $ximage->setAttribute("ordinal", $ordinal);
      $ximages->appendChild($ximage);
    }
  }
  $xchoosecover->appendChild($ximages);  

  $xroot->appendChild($xchoosecover);
}

//--------------------------------------------------------------------------------
// Do choose the album cover
//--------------------------------------------------------------------------------
function choosePhotoCover() {
  global $action;
  global $photo;
  global $pseudo;
  debug("Choosing cover photo $photo of user $pseudo");

  $query = "UPDATE users SET cover='$photo' WHERE pseudo='$pseudo'";
  debugSQL($query);
  mysql_query( $query );

  $action = "mysqpave";
  showMySpacePage(false);
}

?>
